import { _decorator, Node, Vec3 } from 'cc';
import { BallGameBase } from './BallGameBase';
import { Game, HitBallAttribute } from '../core/Game';
import { PlayUser } from '../core/PlayUser';
import { EventManager } from '../core/EventManager';
import { BallAttribute, BallColor } from '../core/BallAttribute';
export class NineBallGame extends BallGameBase {
  constructor(node: Node) {
    super(node);
  }

  public initBallsPosition() {
    const snookerNode = this.billiamNode.getChildByName('SnookerBalls');
    snookerNode.active = false;

    // 初始化球的位置
    const ballsNode = this.billiamNode.getChildByName('Balls');
    // 8球/9球
    ballsNode.active = true;
    const balls = ballsNode.children;
    for (const ball of balls) {
      const pt = BallAttribute.getNinePointByName(ball.name);
      const ba = BallAttribute.getBallByName(ball.name);
      if (ba && ba.index >= 10) {
        EventManager.Instance().Emit('HideBallByName', ball.name);
      } else {
        EventManager.Instance().Emit('InitBallPosition', {
          ballName: ball.name, pos: new Vec3(pt.x, pt.y, pt.z),
        });
      }
    }
    // 设置白球的位置
    EventManager.Instance().Emit('InitMainBallPosition', new Vec3(10, 0, 0));
  }
  // 游戏是否已经结束
  public checkGameEnd() {
    // 9号球进入则结束
    const balls = this.getActiveBalls();
    for (const b of balls) {
      const ba = BallAttribute.getBallByName(b.name);
      if (ba.isNineBall9()) {
        return false;
      }
    }
    return true;
  }
  // 是否进球得分
  public hasHoledScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    const balls = this.getActiveBalls();
    var minIndex = 1000001;
    for (const b of balls) {
      const ba = BallAttribute.getBallByName(b.name);
      if (ba.index <= minIndex) {
        minIndex = ba.index;
      }
    }
    for (var i=hitBallList.length-1; i>=0; i--) {
      if (hitBallList[i].step === step) {
        if (hitBallList[i].ba.isMainWhiteBall()) continue;
        // 有进球就且最小，不然应该违规
        if (hitBallList[i].ba.index < minIndex) return true;
      } else {
        break;
      }
    }
    return false;
  }
  public isIllegalHitBall(firstCollideBall: BallAttribute, hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    const res = super.illeageHitBallResult(firstCollideBall, hitBallList, step, color);
    if (res.isIlleagel) return res;
    const balls = this.getActiveBalls();
    const minHitBall = this.minHitBall(balls);
    if (firstCollideBall && minHitBall && firstCollideBall.index > minHitBall.index) {
      return { isIlleagel: true, message: '未先击中目标球，违规击球' };
    }
    return res;
  }
  // 球桌上当前活跃的球
  public getActiveBalls() {
    const bList = [];
    const balls = this.billiamNode.getChildByName('Balls').children;
    for (const b of balls) {
      if (b.active) {
        bList.push(b);
      }
    }
    return bList;
  }
  // 当前击球 和受保护 的球列表
  public getHitProtectBalls(color: BallColor) {
    const hitBalls = [];
    const protectBalls = [];
    const balls = this.getActiveBalls();
    var minBall = null;
    var minIndex = 1000001;
    for (const b of balls) {
      const ba = BallAttribute.getBallByName(b.name);
      if (ba.index <= minIndex) {
        minIndex = ba.index;
        minBall = b;
      }
    }
    hitBalls.push(minBall);
    for (const b of balls) {
      const ba = BallAttribute.getBallByName(b.name);
      if (ba.index !== minIndex) {
        protectBalls.push(b);
      }
    }
    return { hitBalls, protectBalls };
  }
  public isTargetHitBall(color: BallColor, name: string) {
    const balls = this.getActiveBalls();
    const minHitBall = this.minHitBall(balls);
    const ba = BallAttribute.getBallByName(name);
    return minHitBall.index === ba.index;
  }
  private minHitBall(balls: Node[]) {
    var minBall = null;
    var minIndex = 1000001;
    for (const b of balls) {
      const ba = BallAttribute.getBallByName(b.name);
      if (ba.index <= minIndex) {
        minIndex = ba.index;
        minBall = ba;
      }
    }
    return minBall;
  }

  public getCurrentStepScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    return 0;
  }

  public getUserHitColorText(color: BallColor) {
    const balls = this.getActiveBalls();
    var minBall = null;
    var minIndex = 1000001;
    for (const b of balls) {
      const ba = BallAttribute.getBallByName(b.name);
      if (ba.index <= minIndex) {
        minIndex = ba.index;
        minBall = b;
      }
    }
    if (minBall) {
      return BallAttribute.getChineseName(minBall.name);
    }
    return '最小号球';
  }
  public swapCurrentUserColor(hasExchangeUser: boolean){

  }
  public isCurrentUserWin(side: number, isIlleagel: boolean) {
    // 已经交换击球了，上一个用户违规，当前用户胜利
    if (isIlleagel) return true;
    const isAllBallHoled = this.getActiveBalls().length <= 0;

    // 通过判断黑9球是谁打进的
    const hitBallList = Game.Instance().hitBallList;
    var userId = '';
    var userSide = side;
    for (var i=hitBallList.length-1; i>=0; i--) {
      if (hitBallList[i].ba.isNineBall9()) {
        userId = hitBallList[i].userId;
        break;
      }
    }
    if (userId !== Game.Instance().playUsers[side].userAttribute.userId) {
      userSide = 1 - side;
    }
    if (isAllBallHoled) {
      return (userSide === side);
    }
    return (userSide !== side);
  }
  public penalyScore() {
    return 0;
  }
  public isExcellentLevel(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor, userId: string) {
    // 连续进3个球，优秀 2
    const steps = [0, 0, 0];
    for (var i = hitBallList.length -1; i>=0; i--) {
      const hba = hitBallList[i];
      if (hba.userId === userId) {
        if (hba.step === step) {
          steps[0] += 1;
        } else if (hba.step === step-1) {
          steps[1] += 1;
        } else if (hba.step === step-2) {
          steps[2] += 1;
        } else {
          break;
        }
      } else { break; }
    }
    if (steps[0] >= 1 && steps[1] >= 1 && steps[2] >= 1) return 2;

    return 0;
  }
  // 所有球的信息
  public getAllBallList() {
    const mainBall = this.billiamNode.getChildByName('MainBall').getChildByName('WhiteBall').getChildByName('BallSphereMain');
    const balls = this.billiamNode.getChildByName('Balls').children;
    return this.getNetFrameBallList(mainBall, balls);
  }

  // 所有球节点信息
  public getAllBallNodeList() {
    const bList: Array<Node> = [];
    const mainBall = this.billiamNode.getChildByName('MainBall').getChildByName('WhiteBall').getChildByName('BallSphereMain');
    bList.push(mainBall);
    const balls = this.billiamNode.getChildByName('Balls').children;
    bList.push(...balls);
    return bList;
  }

  public robotRenShu(playUsers: PlayUser[]) { return false; }
}